package org.ycb.ycbisgod.ycbwoxihuanni.security;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.ycb.ycbisgod.ycbwoxihuanni.entity.User;

import java.util.Collection;
import java.util.Collections;

//用于封装用户信息，是Spring Security需要的对象
@Data
public class MyUserDetails implements UserDetails {

    private User user;

    public MyUserDetails(User user) {
        this.user = user;
        this.id = user.getId().toString();
        this.role = user.getRole();
        this.password = user.getPassword();
        this.username = user.getUsername();
    }

    private String id;

    private String role;

    private String password;

    private String username;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
    }
}
